package com.ge.healthcare.autosc.ui.prodiagsscheduler.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.ge.healthcare.autosc.dao.intf.CfgProDiagsScheduleDAO;
import com.ge.healthcare.autosc.dao.intf.HstProdiagsScheduleDAO;
import com.ge.healthcare.autosc.dao.intf.ProDiagsConnectivityDAO;
import com.ge.healthcare.autosc.dto.ScheduleBean;
import com.ge.healthcare.autosc.model.ActRequest;
import com.ge.healthcare.autosc.model.CfgProDiagsSchedule;
import com.ge.healthcare.autosc.model.HstProdiagsSchedule;
import com.ge.healthcare.autosc.ui.prodiagsscheduler.service.intf.ProDiagsConnectivityService;

/*
 *
 * Copyright (C) 2011 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the
 * General Electric Company (GE). You shall not disclose this software and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES. 
 */

public class ProDiagsConnectivityServiceImpl implements ProDiagsConnectivityService {

	private final Logger logger = Logger.getLogger(ProDiagsConnectivityServiceImpl.class);
	private ProDiagsConnectivityDAO proDiagsConnectivityDAO;
	private HstProdiagsScheduleDAO hstProdiagsScheduleDAO;
	private CfgProDiagsScheduleDAO cfgProDiagsScheduleDAO;
	
	public void setHstProdiagsScheduleDAO(HstProdiagsScheduleDAO hstProdiagsScheduleDAO) {
		this.hstProdiagsScheduleDAO = hstProdiagsScheduleDAO;
	}
	
	public void setCfgProDiagsScheduleDAO(
			CfgProDiagsScheduleDAO cfgProDiagsScheduleDAO) {
		this.cfgProDiagsScheduleDAO = cfgProDiagsScheduleDAO;
	}

	public void setProDiagsConnectivityDAO(ProDiagsConnectivityDAO proDiagsConnectivityDAO) {
		this.proDiagsConnectivityDAO = proDiagsConnectivityDAO;
	}

	@Override
	public ScheduleBean viewSchedule(Long systemId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ScheduleBean> loadSchedule(ScheduleBean searchBean) {
		// TODO Remove dummy list and get from db
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		if (searchBean.getSystemId() != null && StringUtils.isNotBlank(searchBean.getModelType())) {
			scheduleBeans.add(new ScheduleBean(searchBean.getSystemId(), searchBean.getModelType(), "HealthPG",
					"Placeholder description for Task 1", 1, 1, 2, 3, 6));
		} else if (StringUtils.isNotBlank(searchBean.getModelType())) {
			scheduleBeans.add(new ScheduleBean("1", searchBean.getModelType(), "HealthPG", "Placeholder description for Task 1",
					3, 1, 2, 3, 6));
		} else if (searchBean.getSystemId() != null) {
			scheduleBeans.add(new ScheduleBean(searchBean.getSystemId(), "GSP", "HealthPG",
					"Placeholder description for Task 3", 1, 2, 3, 6, 4));
		}else{
			Calendar cal = Calendar.getInstance();
			@SuppressWarnings("unused")
			List<ActRequest> actRequestBeans = proDiagsConnectivityDAO.loadSchedule(searchBean);
			for (ActRequest actRequest : actRequestBeans) {
				System.out.println("currDate "+actRequest.getStartDate());
				cal.setTime(actRequest.getStartDate());
				scheduleBeans.add(new ScheduleBean(actRequest.getSystemId(),actRequest.getModelType(), "HealthPG", "Placeholder description for Task 1", 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.WEEK_OF_MONTH)));
			}
		}
		return scheduleBeans;
	}
	
	@Override
	public List<ScheduleBean> loadSProdiagSchedule(ScheduleBean searchBean) {
		// TODO Remove dummy list and get from db
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		List<ActRequest> actRequestBeans = new ArrayList<ActRequest>();
		if (searchBean.getSystemId()!= null ) {
			actRequestBeans = proDiagsConnectivityDAO.getRequestBySystemID(searchBean.getSystemId());
		}
		if (StringUtils.isNotBlank(searchBean.getModelType())) {
			actRequestBeans = proDiagsConnectivityDAO.getRequestByModelType(searchBean.getModelType());

		}
		if (StringUtils.isNotBlank(searchBean.getModelTypeSelect())) {
			actRequestBeans = proDiagsConnectivityDAO.getRequestByModelType(searchBean.getModelTypeSelect());

		}
			Calendar cal = Calendar.getInstance();
			for (ActRequest actRequest : actRequestBeans) {
				System.out.println("currDate "+actRequest.getStartDate());
				cal.setTime(actRequest.getStartDate());
				scheduleBeans.add(new ScheduleBean(actRequest.getSystemId(),actRequest.getModelType(), actRequest.getSystemType(), "Placeholder description for Task 1", 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.WEEK_OF_MONTH),searchBean.getType()));
		}
		return scheduleBeans;
	}

	@Override
	public List<ScheduleBean> compareProDiagsConfig(ScheduleBean searchBean) {
	// TODO Remove dummy list and get from db
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		List<HstProdiagsSchedule> hstProdigasScheduleList = hstProdiagsScheduleDAO.getAllObjects(searchBean);
		for (HstProdiagsSchedule hstProdigasSchedule : hstProdigasScheduleList) {
			scheduleBeans.add(new ScheduleBean(hstProdigasSchedule.getSystemId(),hstProdigasSchedule.getModelType(),hstProdigasSchedule.getTaskName(),
					hstProdigasSchedule.getIterations(),hstProdigasSchedule.getDay(),hstProdigasSchedule.getHour(),hstProdigasSchedule.getMinute(),hstProdigasSchedule.getWeek(),
					new Date()));
		}
		
		List<CfgProDiagsSchedule> cfgProDiagsScheduleList = cfgProDiagsScheduleDAO.getAllObjects(searchBean);
		for (CfgProDiagsSchedule cfgProDiagsSchedule : cfgProDiagsScheduleList) {
			scheduleBeans.add(new ScheduleBean(cfgProDiagsSchedule.getSystemId(), cfgProDiagsSchedule.getModelType(), cfgProDiagsSchedule.getTaskName(),
					cfgProDiagsSchedule.getIterations(), cfgProDiagsSchedule.getDay(), cfgProDiagsSchedule.getHour(), cfgProDiagsSchedule.getMinute(), cfgProDiagsSchedule.getWeek(),
					new Date()));
		}
		/*if(StringUtils.isNotBlank(searchBean.getModelType())){
			scheduleBeans.add(new ScheduleBean("1", searchBean.getModelType(), "HealthPG", 1, 1, 2, 3, 6,new Date()));
			scheduleBeans.add(new ScheduleBean("2", searchBean.getModelType(), "HealthPG", 1, 5, 4, 3, 0,new Date()));
			scheduleBeans.add(new ScheduleBean("3", searchBean.getModelType(), "HealthPG", 1, 2, 3, 6, 4,new Date()));
		}else{
			scheduleBeans.add(new ScheduleBean("1", "MR", "HealthPG", 1, 1, 2, 3, 6,new Date()));
			scheduleBeans.add(new ScheduleBean("2", "CT", "HealthPG", 1, 5, 4, 3, 0,new Date()));
			scheduleBeans.add(new ScheduleBean("3", "PET", "HealthPG", 1, 2, 3, 6, 4,new Date()));
			scheduleBeans.add(new ScheduleBean("4", "GSP", "HealthPG", 1, 4, 5, 4, 0,new Date()));
			scheduleBeans.add(new ScheduleBean("5", "XR", "HealthPG", 1, 2, 8, 6, 2,new Date()));
		}*/
			
		return scheduleBeans;
	}

	@Override
	public List<ScheduleBean> report(ScheduleBean searchBean) {
		// TODO Remove dummy list and get from db
		List<ScheduleBean> scheduleBeans = new ArrayList<ScheduleBean>();
		List<HstProdiagsSchedule> hstProdigasScheduleList = hstProdiagsScheduleDAO.getAllObjects(searchBean);
		int i=1;
		for (HstProdiagsSchedule hstProdigasSchedule : hstProdigasScheduleList) {
			if(StringUtils.isNotBlank(searchBean.getRequestId())){
				scheduleBeans.add(new ScheduleBean(searchBean.getRequestId(), "501902876",hstProdigasSchedule.getUpdatedDate(),"Success","Comments"));
			} else {
				scheduleBeans.add(new ScheduleBean(String.valueOf(i++), "501902876",hstProdigasSchedule.getUpdatedDate(),"Success","Comments"));
			}
		}
		
		List<CfgProDiagsSchedule> cfgProDiagsScheduleList = cfgProDiagsScheduleDAO.getAllObjects(searchBean);
		for (CfgProDiagsSchedule cfgProDiagsSchedule : cfgProDiagsScheduleList) {
			if(StringUtils.isNotBlank(searchBean.getRequestId())){
				scheduleBeans.add(new ScheduleBean(searchBean.getRequestId(), "501902876",cfgProDiagsSchedule.getUpdatedDate(),"Success","Comments"));
			} else {
				scheduleBeans.add(new ScheduleBean(String.valueOf(i++), "501902876",cfgProDiagsSchedule.getUpdatedDate(),"Success","Comments"));
			}
		}
			return scheduleBeans;
	}
	public List<Integer> getDayList() {
		List<Integer> dayList = new ArrayList<Integer>();
		for(int i =1 ; i<31; i++){
			dayList.add(i);
		}
		return dayList;
	}
	public List<Integer> getHourList() {
		List<Integer> hourList = new ArrayList<Integer>();
		for(int i =1 ; i<25; i++){
			hourList.add(i);
		}
		return hourList;
	}
	public List<Integer> getMinuteList() {
		List<Integer> minuteList = new ArrayList<Integer>();
		for(int i =1 ; i<25; i++){
			minuteList.add(i);
		}
		return minuteList;
	}
	public List<Integer> getWeekList() {
		List<Integer> weekList = new ArrayList<Integer>();
		for(int i =1 ; i<8; i++){
			weekList.add(i);
		}
		return weekList;
	}
}
